package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.ResourceEventDao;
import com.bocloud.cmp.entity.resource.ResourceEvent;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 资源事件DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("resourceEventDao")
public class ResourceEventDaoImpl extends BasicDao implements ResourceEventDao {

	@Override
	public List<ResourceEvent> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select * from resource_event where 1=1";
		sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, ResourceEvent.class);
		List<ResourceEvent> resourceEvents = new ArrayList<>();
		for (Object obj : list) {
			ResourceEvent resourceEvent = (ResourceEvent) obj;
			resourceEvents.add(resourceEvent);
		}
		return resourceEvents;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from resource_event where 1=1";
		sql = SQLHelper.buildSql(sql, params, null, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public ResourceEvent save(ResourceEvent resourceEvent) throws Exception {
		return (ResourceEvent) this.saveEntity(resourceEvent);
	}

	@Override
	public ResourceEvent query(Long id) throws Exception {
		String sql = "select * from resource_event where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Object> list = this.queryForList(sql, params, ResourceEvent.class);
		if (list.isEmpty()) {
			return null;
		}
		return (ResourceEvent) list.get(0);
	}

	@Override
	public void update(ResourceEvent resourceEvent) throws Exception {
		this.updateEntity(resourceEvent);
	}

	@Override
	public ResourceEvent queryByVmId(Long vmId) throws Exception {
		String sql = "select * from resource_event where vm_id = :vmId";
		Map<String, Object> params = MapTools.simpleMap("vmId", vmId);
		List<Object> list = this.queryForList(sql, params, ResourceEvent.class);
		if (list.isEmpty()) {
			return null;
		}
		return (ResourceEvent) list.get(0);
	}

}
